package com.china08.yxyapi.api.manage.tec.teaching.live.myClassSchedule;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.jboss.logging.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.model.YxyApiReqModel;

import io.swagger.annotations.ApiOperation;

/**
 * 我的教学-直播课-教师-我的课表
 * @author 赵亚辉
 *
 */
@CrossOrigin
@RestController
public class MyClassScheduleController extends YxyApiBaseController{
	
	private static final Logger logger=Logger.getLogger(MyClassScheduleController.class);
	
	@Autowired
	private JdbcTemplate jdbcTemplate;
	
	@ApiOperation(value = "我的课表查询")
    @RequestMapping(value = "/api_yw/manage/tec/teaching/live/myClassSchedule/list", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel findMyClassScheduleList(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel reqModel) {
		
		RespModel model = new RespModel();

        String teacherId = getProfile(request, response).getId();
        System.err.println("teacherId:"+teacherId);
        String schoolId=getProfile(request, response).getSchoolId();
        System.err.println("schoolId:"+schoolId);
        
        //上课课时
        Integer upClass=0;
        //听课课时
        Integer listenClass=0;
        
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("HH:mm");
        
        List<RespLessonItem> respLessonItemList=new ArrayList<>();
        List<Map<String,Object>> mapList = findLessonItems(teacherId);
        for(Map<String,Object> map:mapList){
        	RespLessonItem item=evalRespLessonItem(map);
        	
        	String lessonTeacherId=item.teacherId;
			if(lessonTeacherId.equals(teacherId)){
				upClass++;
				item.classStatus=1;
			}else{
				listenClass++;
				item.classStatus=2;
			}
			
			item.beginDateFormat=simpleDateFormat.format(item.beginDate);
			item.endDateFormat=simpleDateFormat.format(item.endDate);
			
			respLessonItemList.add(item);
        }
        
		model.list=respLessonItemList;
		model.upClass=upClass;
		model.listenClass=listenClass;
		
		return model;
	}
	
	public RespLessonItem evalRespLessonItem(Map<String, Object> map) {
		
		RespLessonItem item=new RespLessonItem();
		
		Object objId = map.get("id");
    	if(objId!=null){
    		item.id=(String) objId;
    	}
    	Object objName = map.get("name");
    	if(objName!=null){
    		item.name=(String) objName;
    	}
    	Object objLessonId =map.get("lesson_id");
    	if(objLessonId!=null){
    		item.lessonId=(String) objLessonId;
    	}
    	Object objTitle =map.get("title");
    	if(objTitle!=null){
    		item.title=(String) objTitle;
    	}
    	Object objBeginDate =map.get("begin_date");
    	if(objBeginDate!=null){
    		item.beginDate=(Date) objBeginDate;
    	}
    	Object objEndDate =map.get("end_date");
    	if(objEndDate!=null){
    		item.endDate=(Date) objEndDate;
    	}
    	Object objStatus =map.get("status");
    	if(objStatus!=null){
    		item.status=(Integer) objStatus;
    	}
    	Object objTeacherId =map.get("teacher_id");
    	if(objTeacherId!=null){
    		item.teacherId=(String) objTeacherId;
    	}
    	Object objRecordOpen =map.get("record_open");
    	if(objRecordOpen!=null){
    		item.recordOpen=(Integer) objRecordOpen;
    	}
    	
    	return item;
	}

	protected static class ReqModel extends YxyApiReqModel{
		
		
	}
	
	protected static class RespModel {
		
		//课时列表
		public List<RespLessonItem> list;
		//上课课时
		public Integer upClass=0;
		//听课课时
		public Integer listenClass=0;
		
	}
	
	protected static class RespLessonItem{
		
		public String id;
		
		public String lessonId;
		
		public String title;
		
		public Date beginDate;
		
		public Date endDate;
		
		public Integer status;
		// 1:上课；2，听课
		public Integer classStatus;
		//主讲人id
		public String teacherId;
		//格式化后开始时间
		public String beginDateFormat;
		//格式化后结束时间
		public String endDateFormat;
		//录播开启标识
		public Integer recordOpen;
		//课程名称
		public String name;
	}
	
	/**
	 * 查询课时
	 * @param teacherId
	 * @return
	 */
	public List<Map<String, Object>> findLessonItems(String teacherId){
		
		String dataSQL="select lesson.name,lesson_item.lesson_id,lesson_item.id,lesson_item.title,lesson_item.begin_date,lesson_item.end_date,lesson_item.status,lesson.teacher_id,lesson_item.record_open " 
				+" from yxy_live.yxy_live_lesson lesson,yxy_live.yxy_live_lesson_item lesson_item,yxy_live.yxy_live_user_favor userFavor " 
				+" where lesson.id=lesson_item.lesson_id and lesson.id=userFavor.live_lesson_id and userFavor.user_id='"+teacherId+"'"
				+" and lesson.audit_status=1 and lesson.status=1 "
				+" and DATE_FORMAT(lesson_item.begin_date,'%y-%m-%d')=DATE_FORMAT(NOW(),'%y-%m-%d')" 
				+" and DATE_FORMAT(lesson_item.end_date,'%y-%m-%d')=DATE_FORMAT(NOW(),'%y-%m-%d')" 
				+" order by lesson_item.begin_date";
		System.err.println("sql:"+dataSQL);
		List<Map<String, Object>> map = jdbcTemplate.queryForList(dataSQL);
		
		return map;
	}
	

}
